/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Source;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import net.jxta.discovery.DiscoveryService;
import net.jxta.document.MimeMediaType;
import net.jxta.id.ID;
import net.jxta.pipe.PipeService;

/**
 *
 * @author Miguel
 */
class ThreadNotifyShared extends Thread {

    private DiscoveryService discovery;
    private String peerName;
    private Peer peer;
    private String llavePeers;
    private StringEncrypter encriptador;

    public ThreadNotifyShared(DiscoveryService discovery, String peerName, Peer p, String llavePeers) {
        this.discovery = discovery;
        this.peerName = peerName;
        this.peer= p;
        this.llavePeers= llavePeers;
        encriptador= new StringEncrypter();
    }

    @Override
    public void run() {

        long lifetime = 60 * 100 * 1000L;
        long expiration = 60 * 100 * 1000L;
        long waittime = 60 * 1 * 1000L;

        while (true) {
            try {
                File rutaCompartidos = new File("../temp/Compartidos");
                File[] listaArchivos = rutaCompartidos.listFiles();
                if(listaArchivos!=null) {
                for (int i = 0; i < listaArchivos.length; i++) {
                    final File file = listaArchivos[i];
                    if (!file.isDirectory()) {
                        AdvertisementSharedFiles advTutorial = new AdvertisementSharedFiles();

                        advTutorial.setType(PipeService.UnicastType);
                        advTutorial.setID( peer.darPeerID());
                        ID Laidpeer = advTutorial.getID();

                        advTutorial.setName("file");

                        try {
                            advTutorial.setIP(InetAddress.getLocalHost().getHostAddress());
                        } catch (UnknownHostException ignored) {
                        }
                        advTutorial.setNombreArchivo(encriptador.encriptarMensaje(llavePeers,file.getName()));
                        if(peer.darFechaCreacionArchivoTAl(file.getName())!=null){
                            advTutorial.setFechaCreacion(peer.darFechaCreacionArchivoTAl(file.getName()));
                        }
                        String[] lista =peer.darPalabrasClaveArchivoTAl(file.getName());
                               if(lista!=null){
                                advTutorial.setPalabraClave1(encriptador.encriptarMensaje(llavePeers,lista[0]));
                                advTutorial.setPalabraClave2(encriptador.encriptarMensaje(llavePeers,lista[1]));
                                advTutorial.setPalabraClave3(encriptador.encriptarMensaje(llavePeers,lista[2]));
                               }
                        advTutorial.setTamanoArchivo(Long.toString(file.length()));
                        advTutorial.setOSArch(System.getProperty("os.arch"));
                        advTutorial.setHWArch(System.getProperty("HOSTTYPE",System.getProperty("os.arch")));
                        advTutorial.setHWVendor(System.getProperty("java.vm.vendor"));
                        peer.adicionarListaTemp( advTutorial);
                        System.out.println(
                                "Publicando advertisements :" + lifetime + " expiracion :" + expiration + "La ID del peer: " + Laidpeer);
                        System.out.println(advTutorial.getDocument(MimeMediaType.XMLUTF8).toString());
                        discovery.publish(advTutorial, lifetime, expiration);
                    }}}
                try {
                    Thread.sleep(waittime);
                } catch (Exception e) {// ignored
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
